﻿#region Copyright 
/*-------------------------------------------------------------------------
* 命名空间名称/文件名:    SSPivot.Tools.Services/ToolService 
* CLR版本:     4.0.30319.42000
* 机器名称:    DESKTOP-NHMLO8A
* 功 能：       N/A
* 类 名：       ToolService
* 创建时间：  2025/5/24 14:44:58
* 版本号：      v1.0
* 创建人:        xulong
*-------------------------------------------------------------------------*/
#endregion
using IP2Region.Net.Abstractions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
using SSPivot.AspNetCore.Repository;
using SSPivot.AspNetCore.Services;
using SSPivot.Common;
using SSPivot.Common.Cache;
using SSPivot.Common.Helpers;
using SSPivot.Tools.Config;
using SSPivot.Tools.Services.Tools.Dto;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ToolGood.Words;

namespace SSPivot.Tools.Services.Tools
{
    public class ToolService : ApplicationService, IToolService
    {
        #region 接口注入
        private IConfiguration _configuration;
        public ToolService(IConfiguration configuration)
        {       
                _configuration = configuration;
        }
        #endregion

        #region 服务实现
        /// <summary>
        /// 获得IP地址
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public BaseLocationInfo GetIpLocationInfo(string ip)
        {
            var config = _configuration.GetSection("IP2Region").Get<IP2RegionConfig>();// ConfigHelper.GetOptions<IP2RegionConfig>("IP2RegionConfig");
            var locationInfo = new BaseLocationInfo();
            if (config.Enabled)
            {
                var region = AppDependencyResolver.Current.GetScopeService<ISearcher>().Search(ip);
                locationInfo = BaseLocationInfo.Parse(region);
            }
            return locationInfo;
        }

        #region 违规内容

        /// <summary>
        /// 查询是否存在违规内容并进行替换
        /// </summary>
        /// <returns></returns>
        public async Task<string> IllegalWordsReplace(string oldString, char symbol = '*')
        {
            var cache = await Cache.GetAsync<string>(GlobalConfigTool.IllegalWordsCahceName);
            if (string.IsNullOrEmpty(cache))
            {
                IFileProvider fileProvider = AppContextProvider.WebHostEnvironment.ContentRootFileProvider;
                IFileInfo fileInfo = fileProvider.GetFileInfo("illegalWord/IllegalKeywords.txt");

                string fileContent = null;

                using (StreamReader readSteam = new StreamReader(fileInfo.CreateReadStream()))
                {
                    fileContent = await readSteam.ReadToEndAsync();
                }
                cache = fileContent;
                await Cache.SetAsync(GlobalConfigTool.IllegalWordsCahceName, cache);
            }

            WordsMatch wordsSearch = new WordsMatch();
            wordsSearch.SetKeywords(cache.ToString().Split('|', StringSplitOptions.RemoveEmptyEntries));

            var t = wordsSearch.Replace(oldString, symbol);
            return t;
        }


        /// <summary>
        /// 查询是否存在违规内容
        /// </summary>
        /// <returns></returns>
        public async Task<bool> IllegalWordsContainsAny(string oldString)
        {
            var cache = await Cache.GetAsync<string>(GlobalConfigTool.IllegalWordsCahceName);
            if (string.IsNullOrEmpty(cache))
            {
                IFileProvider fileProvider = AppContextProvider.WebHostEnvironment.ContentRootFileProvider;
                IFileInfo fileInfo = fileProvider.GetFileInfo("illegalWord/IllegalKeywords.txt");

                string fileContent = null;

                using (StreamReader readSteam = new StreamReader(fileInfo.CreateReadStream()))
                {
                    fileContent = await readSteam.ReadToEndAsync();
                }
                cache = fileContent;
                await Cache.SetAsync(GlobalConfigTool.IllegalWordsCahceName, cache);
            }

            WordsMatch wordsSearch = new WordsMatch();
            wordsSearch.SetKeywords(cache.Split('|', StringSplitOptions.RemoveEmptyEntries));

            var bl = wordsSearch.ContainsAny(oldString);

            return bl;
        }
        #endregion
        #endregion
    }
}
